/*
 *  WaveFunctionSinXVariableCycle.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/11/20.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include "WaveFunctionSinXVariableCycle.h"
#include "LineFactory.h"
#include "AllExceptions.h"
#include <iostream>

namespace sc { 

	double WaveFunctionSinXVariableCycle::operator() ( double x ) const
	{
		double cycle = GetCycle( x );
		return std::sin( 2.0 * Pi * cycle * ( slopeOfX * x + interceptOfX ) );

	}

	double WaveFunctionSinXVariableCycle::GetMaxOfY() const
	{
		return 1.0;
	}

	double WaveFunctionSinXVariableCycle::GetCycle() const
	{
		return line.GetStartY();
	}

	bool WaveFunctionSinXVariableCycle::operator == ( const WaveFunction& rhs ) const
	{
		if ( this->WaveFunction::operator == ( rhs ) ) {
			const WaveFunctionSinXVariableCycle& ref = dynamic_cast< const WaveFunctionSinXVariableCycle& >( rhs );
			if ( line == ref.line ) {
				return true;
			}
		}
		return false;
	}

	bool WaveFunctionSinXVariableCycle::operator != ( const WaveFunction& rhs ) const
	{
		return not ( * this == rhs );
	}

	bool WaveFunctionSinXVariableCycle::operator < ( const WaveFunction& rhs ) const
	{
		if ( type != rhs.GetType() ) {
			return type < rhs.GetType();
		}
		if ( this->WaveFunction::operator < ( rhs ) ) {
			return true;
		}
		const WaveFunctionSinXVariableCycle& ref = dynamic_cast< const WaveFunctionSinXVariableCycle& >( rhs );
		if ( line != ref.line ) {
			return line < ref.line;
		}
		return false;
	}

	double WaveFunctionSinXVariableCycle::GetCycle( double x ) const
	{
		double cycle;
		if ( x < line.GetStartX() ) {
			cycle = line.GetStartY();
		} else if ( x > line.GetEndX() ) {
			cycle = line.GetEndY();
		} else {
			cycle = line.GetY( x );
		}
		return cycle;
	}

	double WaveFunctionSinXVariableCycle::GetStartCycle() const
	{
		return line.GetStartY();
	}

	double WaveFunctionSinXVariableCycle::GetEndCycle() const
	{
		return line.GetEndY();
	}

	WaveFunctionSinXVariableCycle::WaveFunctionSinXVariableCycle( Line::Type cycleVaryLineType, 
		double startCycle, double endCycle, double xAtStartCycle, double xAtEndCycle )
	  : WaveFunction( Type_SinXVariableCycle, 0, 1.0, 1.0, 0.0 ),
		line( LineFactory::Create( cycleVaryLineType, xAtStartCycle, startCycle, xAtEndCycle, endCycle ) )
	{}

}
